<?php

/**
 * This is the model class for table "{{proposal}}".
 *
 * The followings are the available columns in table '{{proposal}}':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property integer $proposalType
 * @property string $url
 * @property integer $stateid
 * @property string $email
 * @property integer $userid
 * @property string $createddate
 *
 * The followings are the available model relations:
 * @property State $state
 * @property User $user
 */
class Proposal extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{proposal}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('proposalType, stateid, userid', 'numerical', 'integerOnly'=>true),
			array('name, email', 'required','message'=>Yii::t('default','required.{attribute}')),	
			array('name', 'length', 'max'=>256),
			array('description', 'length', 'max'=>2048),
			array('url', 'length', 'max'=>1024),
			array('email', 'length', 'max'=>512),
			array('email','email','message'=>Yii::t('default','email.{attribute}')),		
			array('createddate', 'safe'),
			array('proposalType', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, description, proposalType, url, stateid, email, userid, createddate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'state' => array(self::BELONGS_TO, 'State', 'stateid'),
			'user' => array(self::BELONGS_TO, 'User', 'userid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'name' =>Yii::t('profession_messages', 'profession.name'),
			'description' => 'Description',
			'proposalType' =>  Yii::t('proposal_messages', 'proposalType'),
			'url' => Yii::t('proposal_messages', 'proposal.bug.url'),
			'stateid' => Yii::t('profession_messages', 'profession.state_id'),
			'email' => Yii::t('profession_messages', 'profession.email'),
			'userid' => Yii::t('profession_messages', 'profession.created_by'),
			'createddate' => Yii::t('proposal_messages', 'createdDate'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('proposalType',$this->proposalType);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('stateid',$this->stateid);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('userid',$this->userid);
		$criteria->compare('createddate',$this->createddate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getTypeString(){
		return Yii::t('proposal_messages','proposalType.'.$this->proposalType);
	}
	
	public function getStateString()
	{
		return Yii::t('state_messages', 'state.'.State::model()->findByPk($this->stateid)->name);
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Proposal the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
